﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace HYSoft.Web
{
    public partial class pagetest : System.Web.UI.Page
    {
        public string status_color = "";
        public int flag_area = 0; //0正常，1预警，2报警
        protected void Page_Load(object sender, EventArgs e)
        {
            //验证登陆是否超时
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (!IsPostBack)
                Bind();
        }
        protected void Bind()
        {
            int curpage = Convert.ToInt32(labPage.Text.ToString());
            PagedDataSource pg = new PagedDataSource();
            Int32 uid = (Int32)Session["uid"];
            string str = " user_index = " + uid + "";
            string search = "";
            //如果用户点击查询监测点，则通过模糊查找获取该监测点
            if (Request.QueryString["sreach"] == null || Request.QueryString["sreach"].ToString() == "")
            {

            }
            else
            {
                search = Request.QueryString["sreach"].ToString();
                str = " addr like '%" + search + "%' and [user_index] = " + uid + "";

            }
            DataSet ds = new BLL.usr2monitor().GetList(str);
            pg.DataSource = ds.Tables[0].DefaultView;
            pg.AllowPaging = true;
            //每页显示的记录条数
            pg.PageSize = 5;
            pg.CurrentPageIndex = curpage - 1;
            this.lnkbtnBack.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            this.lnkbtnOne.Enabled = true;
            this.lnkbtnUp.Enabled = true;
            this.lnkbtnOne.ForeColor = Color.Red;
            this.lnkbtnUp.ForeColor = Color.Red;
            this.lnkbtnNext.ForeColor = Color.Red;
            this.lnkbtnBack.ForeColor = Color.Red;
            //为当前页时，上一页和首页不可点击
            if (curpage == 1 && curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
            else if (curpage == 1)
            {
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnUp.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Red;
                this.lnkbtnBack.ForeColor = Color.Red;
            }
            //为最后一页时，最后一页和下一页不可点击
            else if (curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Red;
                this.lnkbtnUp.ForeColor = Color.Red;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
            //最后一页
            this.labBackPage.Text = Convert.ToString(pg.PageCount);
            this.ListView1.DataSource = pg;
            this.ListView1.DataBind();
        }
        /// <summary>
        /// 判断传感器是否报警
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        protected string data_Status(object id)
        {
            string Status = "";
            flag_area = 0;
            string str = " areano = " + int.Parse(id.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(str);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["chuanganqi"].ToString() == "报警")
                { flag_area = 2; }

                else if (ds.Tables[0].Rows[i]["chuanganqi"].ToString() == "预警")
                {
                    if (flag_area != 2)
                        flag_area = 1;
                }
            }
            //根据不同类型去改变字体颜色
            switch (flag_area)
            {
                case 0: Status = "<a style=\"color:green;\">正常</a>"; break;
                case 1: Status = "<strong style=\"color:orange;\">预警</strong>"; break;
                case 2: Status = "<strong style=\"color:red;\">报警</strong>"; break;
            }
            return Status;
        }


        protected string data_Statusid(object id)
        {
            string Status = "";
            //根据每个监测点查找该监测点下的所有传感器节点的数量
            string str = " areano = " + int.Parse(id.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(str);
            Status = ds.Tables[0].Rows.Count + "";
            Model.monitor monitor = new BLL.monitor().GetModel(int.Parse(id.ToString()));
            //更新该监测点下的传感器数量
            monitor.count = ds.Tables[0].Rows.Count;
            if (new BLL.monitor().Update(monitor))
            {
                ds.Clear();
                return Status;
            }
            return "";
        }
        //点击首页的事件
        protected void lnkbtnOne_Click(object sender, EventArgs e)
        {
            this.labPage.Text = "1";
            this.Bind();
        }
        //点击上一页的事件
        protected void lnkbtnUp_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) - 1);
            this.Bind();
        }
        //点击下一页的事件
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) + 1);
            this.Bind();
        }
        //最后一页的事件
        protected void lnkbtnBack_Click(object sender, EventArgs e)
        {
            this.labPage.Text = labBackPage.Text;
            this.Bind();

        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType == ListViewItemType.InsertItem)
            {
                HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("trItem");
                switch (flag_area)
                {//根据flag_area来设置tr的背景颜色
                    case 1:
                        tr.BgColor = "orange";
                        break;
                    case 2:
                        tr.BgColor = "#FF6600";
                        break;
                    default:
                        break;
                }
            }
        }
    }
}